package cn.bob.basic.service.impl;

import cn.bob.basic.mapper.IBaseMapper;
import cn.bob.basic.query.BaseQuery;
import cn.bob.basic.service.IBaseService;
import cn.bob.basic.utils.PageList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * @author bob
 * @data 2022/10/10-16:22
 */
public class BaseServiceImpl<T> implements IBaseService<T> {
    @Autowired
    private IBaseMapper<T> mapper;

    @Override
    public void add(T t) {
        mapper.add(t);
    }

    @Override
    public void delById(Long id) {
        mapper.delById(id);
    }

    @Override
    public void batchDel(List<Long> ids) {
        mapper.batchDel(ids);
    }

    @Override
    public void update(T t) {
        mapper.update(t);
    }

    @Override
    public List<T> findAll() {
        return mapper.findAll();
    }

    @Override
    public T findById(Long id) {
        return (T) mapper.findById(id);
    }

    @Override
    public Integer queryByCount(BaseQuery query) {
        return mapper.queryByCount(query);
    }


    @Override
    public PageList<T> queryByPage(BaseQuery query) {
        if(!StringUtils.isEmpty(query.getKeyword())){
            query.setKeyword(query.getKeyword().trim());
        }
        //查询总条数
        Integer count = mapper.queryByCount(query);
        //查询数据
        List<T> departments = mapper.queryByPage(query);
        return new PageList<T>(departments, count);
    }


}
